package com.sdtv.basemodule.config;

import android.app.Application;

import com.billy.android.loading.Gloading;
import com.sdtv.basemodule.R;
import com.sdtv.basemodule.http.interceptors.ExceptionInterceptor;
import com.sdtv.basemodule.http.interceptors.HeaderInteceptor;
import com.sdtv.basemodule.http.interceptors.ParamInterceptor;
import com.sdtv.basemodule.image.GlideProcessor;
import com.sdtv.basemodule.image.ImageConfig;
import com.sdtv.basemodule.image.ImageLoaderInterface;
import com.sdtv.basemodule.widget.gloading.GloadingAdapter;

import java.util.TreeMap;

import okhttp3.Headers;
import okhttp3.Response;

/**
 * @ProjectName: MediunFusion
 * @Package: com.sdtv.basemodule.config
 * @ClassName: BaseConfig
 * @Description: 应用config配置的基类--
 * 各组件中继承该方法实现app的基本配置，主要是针对网络的配置
 * @Author: 小郭同学
 * @CreateDate: 2019-06-20 11:10
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public abstract class BaseConfig implements AppConfigDelegate {


    private Application application;

    public BaseConfig(Application application) {
        this.application = application;
    }

    @Override
    public HeaderInteceptor getHeaderInterceptor() {
        return new HeaderInteceptor() {
            @Override
            public Headers buildHeaders(Chain chain) {
                Headers.Builder headrBuilder = new Headers.Builder();
                return headrBuilder.build();
            }
        };
    }

    @Override
    public ParamInterceptor getParamInterceptor() {
        return new ParamInterceptor() {
            @Override
            public TreeMap<String, String> dynamic(TreeMap<String, String> dynamicMap) {
                return dynamicMap;
            }
        };
    }

    @Override
    public ExceptionInterceptor getExceptionInterceptor() {
        return new ExceptionInterceptor() {
            @Override
            public boolean isResponseExpired(Response response, String bodyString) {
                return false;
            }

            @Override
            public Response responseExpired(Chain chain, String bodyString) {
                return null;
            }
        };
    }

    @Override
    public Gloading.Adapter getGloadingAdapter() {
        return new GloadingAdapter();
    }

    @Override
    public Application getApplication() {
        return application;
    }

    @Override
    public AppHttpSetting getHttpSetting() {
        return AppHttpSetting.builder()
                .with(application)
                .addInterceptor(getParamInterceptor())
                .build();
    }

    @Override
    public AppSetting getAppSetting() {
        return AppSetting.builder()
                .with(application)
                // 网络配置
                .setHttpSetting(getHttpSetting())
                //Rxjava异常统一配置
                .setErrorListener(getResponseErrorListener())
                // 加载状态处理
                .setGloadingAdapter(getGloadingAdapter())
                .build();
    }

    /**
     * 设置加载图片的占位图
     *
     * @return
     */
    @Override
    public ImageConfig getImageConfig() {
        return ImageConfig.build()
                .defaultImg(R.mipmap.qk_default)
                .errorImg(R.mipmap.loaddata_error)
                .build();
    }

    /**
     * 设置默认的图片加载框架
     *
     * @return
     */
    @Override
    public ImageLoaderInterface getImageProcessor() {
        return new GlideProcessor();
    }


}
